
layer.config({
    skin: 'sc-layer'
})

/**
 * vue app创建
 * @type {{component: component, use: use, create: create}}
 */
window.app = (() => {
    let v_use = [], v_component = [], icons = [];

    function create(el, init) {
        init = init || {};
        const v_app = Vue.createApp(init);

        v_use.forEach(use => v_app.use(...use));
        v_component.forEach(comp => v_app.component(comp.key, comp.component));

        v_app.mount(el);
    }

    function use(...use) {
        v_use.push(use)
    }

    function component(key, component) {
        v_component.push({key, component})
    }

    use(window.ElementPlus, {
        locale: ElementPlusLocaleZhCn,
    });
    for (const [key, comp] of Object.entries(window.ElementPlusIconsVue)) {
        component(key, comp);
        icons.push(key);
    }

    return {
        create,
        component,
        use,
        icons,
    }
})();

/**
 * sc 全局工具
 * @type {{closeCurrentPage(): void, tagTheme: [], axios: {post(*, *): Promise<unknown>, get(*, *): Promise<unknown>}, message: {success: sc.message.success, warning: sc.message.warning, error: sc.message.error, loading: sc.message.loading, info: sc.message.info}}}
 */
window.sc = {
    closeCurrentPage(){
        try{
            parent.layer.close(window.LayerIndex);
        }catch (e){
            console.log(e);
        }
    },
    tagTheme: (() => {
        const theme = [];
        const type = ['primary', 'success', 'warning', 'danger', 'info', 'primary'];
        const effect = ['light', 'plain', 'dark',];
        for (const i1 in effect) {
            for (const i2 in type) {
                theme.push({type: type[i2], effect: effect[i1], size: 'small'})
            }
        }
        return theme;
    })(),
    axios:{
        get(url, params){
            return new Promise((resolve, reject) => {
                window.axios.get(url, {params}).then(res => {
                    resolve(res.data);
                }).catch(err => {
                    if (!reject(err)){
                        sc.message.error(err)
                    }
                });
            });
        },
        post(url, data){
            return new Promise((resolve, reject) => {
                window.axios.post(url, data).then(res => {
                    resolve(res.data);
                }).catch(err => {
                    if (!reject(err)){
                        sc.message.error(err)
                    }
                });
            });
        },
    },
    message: {
        success: function (msg) {
            window.ElementPlus.ElMessage.success(msg);
        },
        error: function (msg) {
            window.ElementPlus.ElMessage.error(msg);
        },
        warning: function (msg) {
            window.ElementPlus.ElMessage.warning(msg);
        },
        info: function (msg) {
            window.ElementPlus.ElMessage.info(msg);
        },
        loading: function (msg) {
            window.ElementPlus.ElMessage.loading(msg);
        },
    },
    copyObjValue(valueObj, targetObj){
        for (const key in targetObj) {
            if (valueObj.hasOwnProperty(key)) {
                targetObj[key] = valueObj[key];
            }
        }
    }
}